package com.atbun.eduservice.controller;


import com.atbun.commonutils.R;
import com.atbun.eduservice.entity.EduSubject;
import com.atbun.eduservice.service.EduSubjectService;
import com.atbun.eduservice.util.TreeNode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/eduservice/subject")
public class EduSubjectController {
    @Autowired
    private EduSubjectService eduSubjectService;

    /**
     * excel导入数据库
     * @param file
     * @return
     */
    @ApiOperation(value = "Excel批量导入")
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file){
        this.eduSubjectService.importSubjectData(file,eduSubjectService);
        return R.ok();
    }

    /**
     * 查询一级与二级分类列表
     * @return
     */
    @GetMapping("treeSubject")
    public R treeSubject(){
//        获取所有课程数据
        List<EduSubject> subjects = this.eduSubjectService.queryAllSubjectForList();

//        构造节点数据
        List<TreeNode> treeNodes = new ArrayList<>();
        for (EduSubject eduSubject : subjects){
            treeNodes.add(new TreeNode(eduSubject.getId(),eduSubject.getParentId(),eduSubject.getTitle(),eduSubject.getGmtCreate(),eduSubject.getGmtModified()));
        }
        List<TreeNode> nodes = TreeNode.TreeNodeBuilder.build(treeNodes,"0");
        return R.ok().data("list",nodes);
    }

}

